
package com.areamodule.servlet;
import com.areamodule.bean.TbArea;
import com.areamodule.dao.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbAreaServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        request.getSession().setAttribute("hashArea", new TbAreaDAO().getTbArea());
        if("add".equals(method)){
        String areaPostCode=request.getParameter("areaPostCode");
        String areaName=request.getParameter("areaName");
        int parentAreaId=Integer.parseInt(request.getParameter("parentAreaId")==null?"0":request.getParameter("parentAreaId"));
        String areaMemo=request.getParameter("areaMemo");
            if(new TbAreaDAO().add(new TbArea(areaPostCode,areaName,parentAreaId,areaMemo,new UtilDAO().decodeDate()))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            
             request.getSession(true).setAttribute("tbAreaList",new TbAreaDAO().getTbAreaAll());
            response.sendRedirect("AreaModule/TbAreaList.jsp");
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Area", "AreaId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbAreaList",new TbAreaDAO().getTbAreaAll());
            response.sendRedirect("AreaModule/TbAreaList.jsp");
        }
        if("edit".equals(method)){
        int areaId=Integer.parseInt(request.getParameter("areaId"));
        String areaPostCode=request.getParameter("areaPostCode");
        String areaName=request.getParameter("areaName");
        int parentAreaId=Integer.parseInt(request.getParameter("parentAreaId"));
        String areaMemo=request.getParameter("areaMemo");

            if(new TbAreaDAO().edit(new TbArea(areaId,areaPostCode,areaName,parentAreaId,areaMemo,new UtilDAO().decodeDate()))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
             request.getSession(true).setAttribute("tbAreaList",new TbAreaDAO().getTbAreaAll());
            response.sendRedirect("AreaModule/TbAreaList.jsp");
        }
        if ("list".equals(method)) {
        	 //request.getSession().setAttribute("hashArea", new TbAreaDAO().getTbArea());
             request.getSession(true).setAttribute("tbAreaList",new TbAreaDAO().getTbAreaAll());
            response.sendRedirect("AreaModule/TbAreaList.jsp");
        	}
        if ("listById".equals(method)) {
             request.getSession(true).setAttribute("tbArea",new TbAreaDAO().getTbAreaByAreaId(Integer.parseInt(request.getParameter("areaId"))));
            response.sendRedirect("AreaModule/TbAreaOperate.jsp");
    	}
        if("showadd".equals(method)){
        	request.getSession(true).setAttribute("tbAreaList",new TbAreaDAO().getTbAreaAll());
            response.sendRedirect("AreaModule/TbAreaOperate.jsp");
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
